package network_programming.np1_socket_and_serversocket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 多线程
 * */
public class EchoServerMultithreading {
    private int port = 9099;        // 监听的端口号
    private final ServerSocket serverSocket;
    public EchoServerMultithreading() throws IOException {
        serverSocket = new ServerSocket(port);
        System.out.println("服务器已启动");
    }
    public void service() {
        while(true) {
            try {
                Socket socket = serverSocket.accept();
                Thread workThread = new Thread(new EchoServerHandler(socket));    // 创建一个工作线程
                workThread.start();     // 启动工作线程
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        new EchoServerMultithreading().service();
    }
}
